package com.kuaidu.ll.quickreadnews.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.kuaidu.ll.quickreadnews.manager.ActivityStackManager;

/**
 * Created by liulei on 2017/7/5.
 * activity的基类
 */

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeLoadView();
        setContentView(getLayoutId());
        beforeInit(savedInstanceState);
        init();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStackManager.getInstance().removeActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityStackManager.getInstance().removeActivity();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private final void init(){
        initView();
        initData();
    }

    /**
     * 加载数据
     */
    protected void initData(){

    }

    /**
     * 加载布局
     */
    protected void initView(){

    }

    /**
     * activity设置视图前需要做的事
     */
    protected void beforeLoadView(){
        ActivityStackManager.getInstance().addActivity(this);
    }

    /**
     * 加载数据与布局之前做的事
     */
    protected void beforeInit(Bundle savedInstanceState) {

    }


    public void startActivity(Class<?> clazz){
        startActivity(clazz,null);
    }

    public void startActivity(Class<?> clazz, Bundle data) {
        Intent intent = new Intent(this,clazz);
        if(data !=null)
            intent.putExtras(data);
        startActivity(intent);
    }

    public void startActivity(Class<?> clazz,Bundle data,int resquestCode){
            Intent intent = new Intent(this,clazz);
        if (data != null)
            intent.putExtras(data);
        startActivityForResult(intent,resquestCode);
    }

    /**
     * 获取需要向activity添加布局的id
     * @return int 布局的id
     */
    protected abstract int getLayoutId();
}
